//
//  Message1TableViewController.m
//  享动
//
//  Created by 沈浩hh on 16/4/6.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "Message1TableViewController.h"
#import "Message1TableViewCell.h"
#import "Message1Model.h"

@interface Message1TableViewController ()

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) NSMutableArray *iconArray;

// 下拉加载更多
@property (nonatomic, assign)NSInteger page;

@end

@implementation Message1TableViewController
- (void)loadData
{
    _page = 1;
    _modelArray = [NSMutableArray array];
    _iconArray = [NSMutableArray array];
    NSDictionary *parDic = @{@"page" : [NSString stringWithFormat:@"%ld", _page], @"pagesize" : @10, @"subject_id" : @0};
    [RequestManager requestWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" requestType:RequestTypePost parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *dataArray = dic[@"data"];
        
        for (NSDictionary *dataDic in dataArray) {
            Message1Model *model = [[Message1Model alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [_modelArray addObject:model];
            [_iconArray addObject:model.cover_img];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = KHEIGHT / 2 / 3.5;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Message1TableViewCell" bundle:nil] forCellReuseIdentifier:@"Message1TableViewCell"];
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 数据请求
     [self loadData];
    
    // 上拉加载更多
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        // 加载更多
        [self loadMoreData];
    }];
}
- (void)loadMoreData
{
    _page += 1;
    NSDictionary *parDic = @{@"page" :  [NSString stringWithFormat:@"%ld", _page], @"pagesize" : @10, @"subject_id" : @0};
    [RequestManager requestWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" requestType:RequestTypePost parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *dataArray = dic[@"data"];
        
        for (NSDictionary *dataDic in dataArray) {
            Message1Model *model = [[Message1Model alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [_modelArray addObject:model];
            [_iconArray addObject:model.cover_img];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Message1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Message1TableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _modelArray[indexPath.row];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:_iconArray[indexPath.row]]];
    return cell;

}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
